﻿/* ***********************************************
* Author:          陶李林
* Team:            晶奇公司研发部
* Created Time:    2010-8-2 10:39:37
* CopyRight:       晶奇公司版权所有 保留一切权利
* NameSpace:       WebDataControls
* Class:           WebUIHelper
* ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WebDataControls
{
    public static class WebUIHelper
    {
        /// <summary>
        /// 获取子控件控件的的model值
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TableName"></param>
        /// <param name="root"></param>
        public static object GetControlsModel(object model, 
            string TableName, System.Web.UI.Control root)
        {
            foreach (System.Web.UI.Control control in root.Controls)
            {

                if (control is IDataControl)
                {
                    IDataControl dataControl = (control as IDataControl);
                    if (dataControl.TableName.Equals(TableName))
                    {
                        dataControl.GetDataValue(ref model);
                    }
                }
                else if (control.Controls.Count > 0)
                    model = GetControlsModel(model, TableName, control);
            }
            return model;
        }

        /// <summary>
        /// 通过model将该控件的子控件赋值
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TableName"></param>
        /// <param name="root"></param>
        public static void SetControlsValue(object model, string TableName, 
            System.Web.UI.Control root)
        {
            if (model == null) return;
            foreach (System.Web.UI.Control control in root.Controls)
            {
                if (control is IDataControl)
                {
                    IDataControl dataControl = (control as IDataControl);
                    if (dataControl.TableName.Equals(TableName))
                    {
                        dataControl.SetDataValue(model);
                    }
                }
                else if (control.Controls.Count > 0)
                    SetControlsValue(model, TableName, control);

            }
        }

        /// <summary>
        /// 获取实体字典类型
        /// </summary>
        /// <param name="model"></param>
        /// <param name="fieldTypeName"></param>
        /// <returns></returns>
        public static Type GetModelFieldType(object model, string fieldTypeName)
        {
            Type type = model.GetType()
                .GetProperty(fieldTypeName, 
                    BindingFlags.Public | BindingFlags.Instance)
                .PropertyType;
            if (type.GetProperty("HasValue", 
                BindingFlags.Public | BindingFlags.Instance) != null)
            {
                type = type.GetProperty("Value", 
                    BindingFlags.Public | BindingFlags.Instance)
                    .PropertyType;
            }
            return type;
        }
    }
}
